#ifndef RORPC_H
#define RORPC_H

#include <QObject>
#include <QtQmlIntegration>
#include "ROSyncObject.h"
#include "ROPlayer.h"
class RORPC:public ROSyncObject
{
    Q_OBJECT
    QML_ELEMENT
public:
    enum Policy{
        CallOnSource,//在源对象上执行，即role==Host或Individual
        CallOnAllPlayers,//在所有的具有Player的端上执行
        CallOnBroadcast,//在服务端和所有的客户端上执行,对于Individual来说和CallOnSource相同
        CallOnPlayer//在Player属性对应的客户端内执行
    };
    Q_ENUM(Policy);

    enum CallingMode//RPC调用方式
    {
        Safe,//安全模式，确保调用可达不丢包
        Fast//快速模式，可能会丢包，而且不推荐携带大量参数，会严重提高丢包率
    };
    Q_ENUM(CallingMode);

public:
    RORPC(QObject* parent = nullptr);

    void initialize() override;

    ROPlayer *player() const;
    void setPlayer(ROPlayer *newPlayer);

    bool active() const;
    void setActive(bool newActive);

    //开始执行RPC调用
    Q_INVOKABLE void call(QString funName,const QVariantList& params = {});

    Policy policy() const;
    void setPolicy(Policy newPolicy);

    CallingMode mode() const;
    void setMode(const CallingMode &newMode);

signals:
    void playerChanged();

    void activeChanged();

    void policyChanged();

    void modeChanged();

private:
    bool m_active = true;
    ROPlayer* m_player = nullptr;
    Policy m_policy = CallOnSource;
    CallingMode m_mode = Safe;


    Q_PROPERTY(ROPlayer *player READ player WRITE setPlayer NOTIFY playerChanged FINAL)
    Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged FINAL)
    Q_PROPERTY(Policy policy READ policy WRITE setPolicy NOTIFY policyChanged FINAL)
    Q_PROPERTY(CallingMode mode READ mode WRITE setMode NOTIFY modeChanged FINAL)
};

#endif
